<?php
declare (strict_types=1);

namespace app\admin\service\market;

use app\admin\logic\system\Config as SystemConfig;
use app\common\exception\LogicException;
use app\common\service\BaseService;
use app\school\logic\school\admin\SchoolSplitConfig as SchoolSplitConfigLogic;
use app\school\model\school\admin\SchoolLevel as SchoolLevelModel;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\page\PageForm;
use think\Request;

/**
 * 分成配置
 */
class Split extends BaseService
{
    /**
     * 分成配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @return   [type]                         [description]
     */
    public function config(Request $request)
    {
        $key = 'split.config';
        // 多机构
        $mhm = config('eduline.appinfo.mhm');
        if ($request->isPost()) {
            $data = $request->post();
            // 隐藏挂载机构配置数据
            unset($data['mount_school']);
            // 计算总和
            if (array_sum($data) != 100) {
                throw new LogicException('分成比例总和需为100');
            }
            if ($mhm) {
                // 检测当前机构+平台配置的分成比例,是否超过已有机构等级
                $sum = $data['platform'] + $data['school'];
                $max = app(SchoolLevelModel::class)->max('proportion');
                if ($sum < $max) {
                    throw new LogicException('当前平台与机构分成总和最低需为' . $max . '，或降低机构等级最高分成');
                }
            }

            SystemConfig::set($key, $data);

            return $this->parseToData([], 1, '保存成功');
        }

        $datas    = [];
        $disabled = false;
        $status   = false;
        if (request()->is_platform || !$mhm) {
            $url = '/admin/market/split/config';
        } else {
            $config   = app(SchoolSplitConfigLogic::class)->getSplitConfig();
            $datas    = $config->getAttr('value');
            $status   = $config->getAttr('status');
            $disabled = $status == 2;
            $url      = '/school/admin/split/config';
        }

        if (!$datas) {
            $datas = SystemConfig::get($key, []);
        }

        $fields = [
            // 平台
            'platform'           => FormItem::make()->title('平台')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
            // 机构
            'school'             => FormItem::make()->title('机构')->slot('%')->css(['width' => '146px'])->required()->visible($mhm)->disabled($disabled),
            // 挂载机构
            // 'mount_school'       => FormItem::make()->title('挂载机构')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
            // 讲师
            'teacher'            => FormItem::make()->title('讲师')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
            // 分享者
            'share'              => FormItem::make()->title('分享者')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
            // 1级分销员
            'distributor_first'  => FormItem::make()->title('1级分销员')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
            // 2级分销员
            'distributor_second' => FormItem::make()->title('2级分销员')->slot('%')->css(['width' => '146px'])->required()->disabled($disabled),
        ];

        $form = new PageForm();
        // 是否显示审核状态
        if ($status !== false && !request()->is_platform && $mhm) {
            $fields['status'] = FormItem::make('custom')->title('当前状态');
            $form->status     = $status;
        }

        $form->pageKey = $fields;

        $form->datas = $datas;

        $form->saveMethod  = 'POST';
        $form->savePostUrl = $url;

        return $form->send();

    }
}
